package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.ActorDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.repository.ActorMenuRepository;
import com.hualude.roadsign.repository.ActorRepository;
import com.hualude.roadsign.service.ActorService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu-jiaHui on 2017/4/7.
 */
@Service
public class ActorServiceImpl implements ActorService {
    @Resource
    private ActorRepository actorRepository;
    @Resource
    private ActorMenuRepository actorMenuRepository;

    public DTResponseDto getActor4Dt(DTRequestDto dtRequestDto) throws Exception {
        List<ActorDto> actorDtos = actorRepository.getActor4Dt(dtRequestDto.getSearch());
        Integer count = actorRepository.getActorCount4Dt(dtRequestDto.getSearch());
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, actorDtos);
        return dtResponseDto;
    }

    @Transactional
    public Integer updateActorById(ActorDto actorDto) throws Exception {
        return actorRepository.updateActorById(actorDto.getActorName(), actorDto.getActorRemarks(), actorDto.getActorId());
    }

    @Transactional
    public Integer addActor(ActorDto actorDto) throws Exception {
        return actorRepository.save(actorDto.toBean()).getActorId();
    }

    @Transactional
    public Integer deleteActorById(Integer actorId) throws Exception {
        actorMenuRepository.deleteActorMenuByActorId(actorId);
        return actorRepository.deleteActorById(actorId);
    }


}
